﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.SERVICE.DAL;
using KPIS.GERP.SERVICE.Demo.Classes;
using KPIS.GERP.SERVICE.Model;
using Telerik.Web.UI;

namespace KPIS.GERP.SERVICE.Demo.Ekk.MySampleFlow.UserControls
{
    public partial class LoginBlock : System.Web.UI.UserControl
    {
        private readonly Employee _employeeDal = new Employee();
        private readonly User _userDal = new User();
        private SsMng ssMng = new SsMng();
        public string Mode
        {
            get { return Request["mode"]; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadCmbRadUser();
            }
        }

        protected void cmbRadUser_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            Login(e.Value); 
            HttpContext.Current.Response.Redirect(HttpUtility.UrlDecode(Request["currenturl"]) ?? "CreateNewRequest.aspx?mode=vs");
        }

        private void LoadCmbRadUser()
        {
            var ds = _employeeDal.GetEmployee(null, null, null, null, null, null, null, true, false);
            ds.Insert(0, new hrm_ps_employees {first_name = "กรุณาเลือก"});
            cmbRadUser.DataSource = ds;
            cmbRadUser.DataBind();
        }

        #region Login

        public void Login(string empSeq)
        {
            if (String.IsNullOrEmpty(empSeq))
                return;
            
            var listEmp = _employeeDal.GetEmployee(empSeq, null, null, null, null, null, null, true, false);
            var listUser = _userDal.GetUser(null, empSeq, null, null, true);

            var emp = listEmp[0];
            if(listUser.Count == 0) return;
            var user = listUser[0];
             
            ssMng.SetLoginSession(Convert.ToString(user.user_seq), Convert.ToString(emp.emp_seq), user.user_code,
                null, null, true, emp.full_name);
        }

        #endregion Login

        #region prop Current

        public string CurrentEmpSeq
        {
            get { return ssMng.EmpSeq; }
            set
            {
                ssMng.EmpSeq = value;
            }
        }

        public string CurrentEmpFullName
        {
            get { return ssMng.UserFullName; }
            set
            {
                ssMng.UserFullName = value;
            }
        }

        #endregion prop Current

        protected void link1_Click(object sender, EventArgs e)
        {
            RedirectMode(Mode == "vs" ? "iis" : "vs");
        }

        public void RedirectMode(string mode)
        {
            if (mode == null)
                mode = "vs";

            HttpContext.Current.Response.Redirect("CreateNewRequest.aspx?mode=" + mode);
        }
    }
}